﻿using DevExpress.XtraEditors;
using DXApplicationTangche.UC.面料审核.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.Utils.UC
{
    public partial class XtraUserControl1 : DevExpress.XtraEditors.XtraUserControl
    {
        public XtraUserControl1()
        {
            InitializeComponent();
        }
        public XtraUserControl1(String sessionId, String id, Dictionary<String, String> parameter)
        {
            InitializeComponent();
            //  取数据
            DataTableQueryConfigDTO dataTableQueryConfigDTO = DataTableService.getDataBysessionId(sessionId, id, parameter);
            //  转dataTable
            DataTable dt = DataTableService.ToDataTable(dataTableQueryConfigDTO.datas, dataTableQueryConfigDTO.metaDataDTOS);
            //  绑定数据
            this.gridControlDataTable.DataSource = dt;
            //  显示设置
            this.addColumn(dataTableQueryConfigDTO.metaDataDTOS);
        }
        
        private void addColumn(List<MetaDataDTO> metaDatas)
        {
            for (int i = 0; i < metaDatas.Count; i++)
            {
                DevExpress.XtraGrid.Columns.GridColumn col = new DevExpress.XtraGrid.Columns.GridColumn();
                col.Caption = metaDatas[i].caption;
                col.FieldName = metaDatas[i].name;
                col.Name = metaDatas[i].name;
                col.Visible = metaDatas[i].visible;
                col.MinWidth = 180;
                //col.VisibleIndex = i;
                this.gridViewDataTable.Columns.Add(col);
            }
        }

        public void exportExcel()
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridViewDataTable.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }

}
